<?php
$this->Html->addCrumb('Articles', '/admin/articles');
$this->Html->addCrumb('Edit');
$this->Html->scriptStart(array('inline' => false));
echo '
      $(function(){
        $("#ArticleAdminEditForm").TWS_Form( {
          validate : {
          rules : {
            ArticleTitle : {required : true, help : "This is title of the article"},
            ArticleBody : {required : true, help : "This is body of the article"}
          }});
        
         $("#deleteBtn").click(function(event){
          if(!$("#deleteBtn").attr("data-delete-confirm")) {
          $("#deleteBtn").attr("data-delete-confirm","");
            $.TWS_Overlay("confirm", {title : "Delete confirmation", message : "Are you sure you want to delete this record?", buttons : {ok :{onclick : function () { $.TWS_Overlay("hide"); $("#deleteBtn").attr("data-delete-confirm",true).trigger("click");}}}});
            event.preventDefault();
          }
        });
         $("#deleteImage").click(function(event){
            var $link = $(this);
            $.TWS_Overlay("confirm", {title : "Delete confirmation", message : "Are you sure you want to delete this image?", buttons : {ok :{onclick : function () { $.TWS_Overlay("hide"); location.href=$link.attr("href");}}}});
            event.preventDefault();
        });
      });
       ';
$this->Html->scriptEnd();

?>


<h1>Edit Article</h1>
<?php echo $this->Session->flash();?>
<?php echo '<div id="actionLink">'.$this->Html->link('Back to list of Articles',array('action'=>'index')).'</div>'; ?>

<?php echo $this->element('errors', array('errors' => isset($errors)? $errors : null)); ?>
<?php

echo $this->TWSForm->create('Article',  array(
    'inputDefaults' => array(
        'div' => array('class'=>'row')),
        'class'=>'inlineForm',
        'type'=>'file'));
echo $this->TWSForm->input('title', array('name'=>'ArticleTitle', 'type'=>'text', 'label'=>'<span class="required">*</span>Title'));
echo $this->TWSForm->input('body', array('name'=>'ArticleBody', 'type'=>'textarea', 'rows'=>'10', 'cols'=>'60' ,'label'=>'Body'));

$record = $this->request->data['Article'];
if(!empty($record['image_path'])){
  echo '<p>'.$this->Html->image($record['image_path'], array('alt' => $record['title'])).'</p>';
  echo '<p>'.$this->Html->link('Delete image', array('controller' => 'Articles', 'action' => 'deleteImage', 'admin'=>true, $record['id']), array("title"=>"Delete this image" , "id"=>"deleteImage")).'</p>';
  echo $this->TWSForm->input('_image_path', array('type'=>'hidden', 'name'=>'_image_path', 'value'=>$record['image_path']));
  } else {
  echo $this->TWSForm->input('image_path', array('type'=>'file', 'label'=>'Upload image', 'name'=>'ArticleImagePath'));
}



echo $this->TWSForm->submit('Update', array('id'=>'editBtn', 'name'=>'editBtn', 'class'=>'button', 'div'=>false));
echo $this->TWSForm->submit("Delete", array('id'=>'deleteBtn','name'=>'deleteBtn', 'class'=>'button', 'div'=>false));
echo $this->TWSForm->end();

?>